package vg.modules.conveyor.components;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import vg.services.data_base_manager.data.graph.Attribute;
import vg.services.data_base_manager.data.graph.Edge;
import vg.services.data_base_manager.realization.DataBaseTypeParser;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.main_manager.MainManager;

public class ChangeEdgeParametersDialog {
	// Swing components
	private static JComponent[] dialogComponents = null;
	private static JLabel inputLabel, outputLabel;
	private static JTextField inputTextField, outputTextField;
	
	// Mutex
	private static final Object generalMutex = new Object();
	
	public static void show(final Edge edge, final AGraphView graphView) {
		synchronized (generalMutex) {
			createComponents4Dialog(edge);
			if (JOptionPane.showConfirmDialog(MainManager.view.getMainFrame(), dialogComponents, "Edge settings", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == 0) {
				String inputNumber = inputTextField.getText();
				String outputNumber = outputTextField.getText();
				
				int inputId = MainManager.model.createAttributeHeader4Edge(edge.getStorableId(), "input_number", inputNumber, DataBaseTypeParser.string2class(DataBaseTypeParser.STRING));
				int outputId = MainManager.model.createAttributeHeader4Edge(edge.getStorableId(), "output_number", outputNumber, DataBaseTypeParser.string2class(DataBaseTypeParser.STRING));
				
				final Attribute inputAttr = MainManager.model.getAttribute(inputId);
				final Attribute outputAttr = MainManager.model.getAttribute(outputId);
				
				Thread t = new Thread(new Runnable() {
					@Override
					public void run() {
						if (graphView != null) {
							graphView.getLowLevelGraphEditor().addAttribute4Edge(inputAttr, edge);
							graphView.getLowLevelGraphEditor().addAttribute4Edge(outputAttr, edge);
						}
					}
				});
				t.setDaemon(true);
				t.start();
			}
		}
	}
	
//=============================================================================
//-----------------PRIVATE METHODS---------------------------------------------
	private static void createComponents4Dialog(final Edge edge) {
		synchronized (generalMutex) {
			if (!SwingUtilities.isEventDispatchThread()) {
				try {
					SwingUtilities.invokeAndWait(new Runnable() {
						@Override
						public void run() {
							synchronized (generalMutex) {
								createComponents4Dialog(edge);									
							}
						}
					});
				} catch (Exception ex) {
					MainManager.log.printException(ex);
				}
			} else {
				inputLabel = new JLabel("Input number:");
				inputTextField = new JTextField(0);
				
				outputLabel = new JLabel("Output number:");
				outputTextField = new JTextField(0);
				
				dialogComponents = new JComponent[4];
				dialogComponents[0] = inputLabel;
				dialogComponents[1] = inputTextField;
				dialogComponents[2] = outputLabel;
				dialogComponents[3] = outputTextField;
			}
		}
	}
}
